package com.ruoyi.ruoyixingyue.modules.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.page.PageData;
import com.ruoyi.ruoyixingyue.modules.api.entity.File;
import com.ruoyi.ruoyixingyue.modules.api.service.IFileService;
import com.ruoyi.ruoyixingyue.modules.api.vo.FileListVo;
import com.ruoyi.ruoyixingyue.modules.utils.Condition;
import com.ruoyi.ruoyixingyue.modules.utils.QueryPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 文件表，可以是普通文件、图片等 前端控制器
 * </p>
 *
 * @author lzz
 * @since 2024-04-06
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private IFileService fileService;

    @PostMapping("/file/list/API_001")
    public R fileListAPI_001(@RequestBody Condition<FileListVo> condition) {
        QueryPage queryPage = condition.getPage();
        FileListVo listVo = condition.getCondition();
        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Objects.nonNull(listVo.getId()), File::getId, listVo.getId())
                .like(StringUtils.isNotEmpty(listVo.getFilename()), File::getFilename, listVo.getFilename())
                .eq(Objects.nonNull(listVo.getType()), File::getType, listVo.getType())
                .eq(Objects.nonNull(listVo.getPlatform()), File::getPlatform, listVo.getPlatform());
        Page<File> resultPage = new Page<File>();
        resultPage.setCurrent(queryPage.getCurrent()).setSize(queryPage.getSize());
        fileService.page(resultPage, queryWrapper);
        return R.ok(new PageData<>(resultPage.getRecords(), resultPage.getTotal()));
    }

    @PostMapping("/file/getById/API_002/{id}")
    public R fileGetByIdApi_002(@PathVariable("id") Long id) {
        File order = fileService.getById(id);
        return R.ok(order);
    }

}


